<template>
  <el-dialog
    v-loading="isLoading"
    class="dialog"
    :title="isEdit ? '跟着练审核详情' : '上传跟着练'"
    :visible.sync="dialogVisible"
    width="800px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="save"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <div class="common">
        <el-form-item prop="pfJobName" label="作业名称:">
          {{ form.pfJobName }}
        </el-form-item>
        <el-form-item label="适合人群:" prop="pfScId">
          <div>
            年级: <b>{{ list }}</b>
            <span style="margin-left: 18px"
              >性别:
              <b>{{
                form.pfSex == 1 ? "男" : form.pfSex == 2 ? "女" : "全性别"
              }}</b>
            </span>
          </div>
        </el-form-item>
      </div>
      <div class="common">
        <el-form-item label="简介:" prop="pfIntroduction">
          {{ form.pfIntroduction }}
        </el-form-item>
      </div>
      <div class="common">
        <el-form-item label="封面图" prop="pfImgUrl">
          <div style="height: 150px">
            <img
              style="
                width: auto;
                height: auto;
                max-height: 100%;
                max-width: 100%;
              "
              v-if="form.pfImgUrl"
              :src="$store.state.user.BASE_URL + form.pfImgUrl"
              alt=""
            />
          </div>
        </el-form-item>
      </div>
      <div class="common">
        <el-form-item label="视频:" prop="pfVideoUrl">
          <div style="height: 150px">
            <video
              ref="video"
              controls=""
              style="width: 270px; height: 100%; object-fit: fill"
              webkit-playsinline="true"
              x-webkit-airplay="true"
              playsinline="true"
              x5-video-player-type="h5"
              x5-video-orientation="h5"
              x5-video-player-fullscreen="true"
              preload="auto"
              loop="loop"
              poster=""
              muted="muted"
              :src="$store.state.user.BASE_URL + form.pfVideoUrl"
            ></video>
          </div>
        </el-form-item>
      </div>
      <div class="common">
        <el-form-item label="循环:" prop="checkedBoxWeek">
          <el-checkbox-group v-model="form.checkedBoxWeek" size="small">
            <el-checkbox
              disabled
              border
              style="margin-right: 1px"
              v-for="item in week"
              :label="item.value"
              :key="item.value"
              >{{ item.label }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
      </div>
      <el-form-item label="审核" v-if="form.pfCheckStatus == 0">
        <el-radio v-model="radio" label="1">通过</el-radio>
        <el-radio v-model="radio" label="2">不通过</el-radio>
      </el-form-item>
      <el-form-item label="不通过原因" prop="textarea" v-if="this.radio == 2">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          resize="none"
          v-model="form.textarea"
          style="width: 500px"
        >
        </el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer" v-if="show">
      <el-button size="small" @click="save()">取 消</el-button>
      <el-button
        size="small"
        :loading="buttonLoading"
        type="primary"
        @click="submit()"
        >确 定</el-button
      >
    </span>
    <span slot="footer" class="dialog-footer" v-if="!show"> </span>
  </el-dialog>
</template>

<script>
import {
  homeworkList,
  homeworkDetails,
  editHomework,
  selectGradeListByRole,
} from "@/api/followThePractice/index.js";
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      options: [],
      radio: "1",

      options1: [
        {
          value: "1",
          label: "男",
        },
        {
          value: "2",
          label: "女",
        },
        {
          value: "3",
          label: "全性别",
        },
      ],

      week: [
        {
          label: "周一",
          value: "1",
        },
        {
          label: "周二",
          value: "2",
        },
        {
          label: "周三",
          value: "3",
        },
        {
          label: "周四",
          value: "4",
        },
        {
          label: "周五",
          value: "5",
        },
        {
          label: "周六",
          value: "6",
        },
        {
          label: "周日",
          value: "7",
        },
      ],
      loading1: false,
      isLoading: false,
      loading: false,
      buttonLoading: false,
      gradelist: [],
      form: {
        pfJobName: "",
        pfImgUrl: "",
        pfVideoUrl: "",
        pfScId: "",
        pfSex: "",
        pfIntroduction: "",
        checkedBoxWeek: [],
        textarea: "",
      },
      rules: {
        textarea: [
          {
            validator: this.validatePhonea,
            trigger: "blur",
          },
        ],
      },
      rules: {
        textarea: [
          {
            required: true,
            message: "请输入审核原因",
            trigger: "blur",
          },
        ],
        pfJobName: [
          {
            required: true,
            message: "请输入学生姓名",
          },
        ],
        pfIntroduction: [
          {
            required: true,
            message: "请输入简介",
          },
        ],
        pfScId: [
          {
            validator: this.validatePhone,
            trigger: "change",
            region: [
              { required: true, message: "请选择活动区域", trigger: "" },
            ],
          },
        ],
        pfImgUrl: [
          {
            required: true,
            message: "请上传图片",
          },
        ],
        pfVideoUrl: [
          {
            required: true,
            message: "请上传视频",
          },
        ],
        checkedBoxWeek: [
          {
            required: true,
            message: "请选择日期",
          },
        ],
      },
    };
  },
  mounted() {
    this.getselectGradeListByRole();
  },
  computed: {
    show() {
      if (this.item.pfCheckStatus == 1) {
        return false;
      }
      if (this.item.pfCheckStatus == 2) {
        return false;
      } else {
        return true;
      }
    },
    list() {
      if (this.gradelist.length && this.form.pfScId) {
        return this.gradelist.filter(
          (item) => item.gradeId === this.form.pfScId
        ).length &&
          this.gradelist.filter((item) => item.gradeId === this.form.pfScId)[0]
            .gradeName
          ? this.gradelist.filter(
              (item) => item.gradeId === this.form.pfScId
            )[0].gradeName
          : "";
      }
    },
  },
  watch: {
    isEdit: {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.getStudentMessage(this.item.pfId);
        }
      },
    },
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
    item: {
      type: Object,
      default: {},
    },
    dialogVisible: {
      type: Boolean,
    },
  },

  methods: {
    validatePhonea(rule, value, callback) {
      if (!this.form.textarea && this.radio == 2) {
        callback(new Error("请说明原因"));
      } else {
        callback(); // 通过
      }
    },
    getselectGradeListByRole() {
      let data = {
        userId: this.$store.state.user.USER_INFO.id,
        typeId: "0",
        shId: this.item.pfShId,
      };
      selectGradeListByRole(data).then((res) => {
        if (res.code == 200) {
          this.gradelist = res.result;
        }
      });
    },
    validatePhone(rule, value, callback) {
      if (!this.form.pfSex && !this.form.pfScId) {
        callback(new Error("请选择"));
      }
      if (!this.form.pfScId) {
        callback(new Error("请选择班级"));
      }
      if (!this.form.pfSex) {
        callback(new Error("请选择性别"));
      } else {
        callback(); // 通过
      }
    },
    beforeUpload(file) {
      this.loading = true;
      const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
      const types = ["jpeg", "jpg", "png"];
      if (!types.includes(fileSuffix)) {
        this.$message.error(`文件只能是 ${types.join()} 格式!`);
        this.loading = false;
      }
      return types.includes(fileSuffix);
    },
    videoUpload(e) {
      if (e.code === 200) {
        this.form.pfVideoUrl = e.result.pic;
      } else if (e.code === 401) {
        this.$router.replace("/");
        this.$message.error("您的身份信息已过期，请重新登录");
        sessionStorage.removeItem("userInfo");
        sessionStorage.removeItem("token");
        this.$store.commit("user/SET_TOKEN", "");
      } else {
        this.$message.error(e.message);
      }
      this.loading1 = false;
    },
    beforeUpload1(file) {
      this.loading1 = true;
      const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
      const types = [
        "avi",
        "mov",
        "mpg",
        "mpeg",
        "rmvb",
        "mp4",
        "flv",
        "rm",
        "mkv",
      ];
      if (!types.includes(fileSuffix)) {
        this.$message.error(`文件只能是 ${types.join()} 格式!`);
        this.loading1 = false;
      }
      return types.includes(fileSuffix);
    },
    validator() {
      this.$refs.form.validate(() => {});
    },
    save(done) {
      this.$emit("close", done);
    },
    picUpload(e) {
      if (e.code === 200) {
        this.form.pfImgUrl = e.result.pic;
      } else if (e.code === 401) {
        this.$router.replace("/");
        this.$message.error("您的身份信息已过期，请重新登录");
        sessionStorage.removeItem("userInfo");
        sessionStorage.removeItem("token");
        this.$store.commit("user/SET_TOKEN", "");
      } else {
        this.$message.error(e.message);
      }
      this.loading = false;
    },
    getStudentMessage(id) {
      this.isLoading = true;
      let data = { pfId: id };
      homeworkDetails(data).then((response) => {
        if (response.code === 200) {
          this.form = response.result;
          // this.form.checkedBoxWeek=response.result.pfCurrentCycle.split(',').concat([]);
          this.$set(
            this.form,
            "checkedBoxWeek",
            response.result.pfCurrentCycle.split(",").concat([])
          );
        }
        this.isLoading = false;
      });
    },
    submit() {
      this.$refs.form.validate((e) => {
        if (e) {
          this.buttonLoading = true;
          this.dialogVisible = false;
          let data = {
            pfId: this.form.pfId,
            pfCheckStatus: this.radio,
            pfCheckInfo: this.form.textarea,
          };
          editHomework(data).then((res) => {
            this.buttonLoading = false;
            if (res.code == 200) {
              this.$emit("submit");
              this.$message.success("操作成功");
            }
          });
        }
      });
    },
    addPhone() {
      this.form.phoneNum.push({ id: this.form.phoneNum.length, mobile: "" });
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  &::-webkit-scrollbar {
    width: 10px;
    border-radius: 16px;
  }

  &::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 16px;
  }

  &::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 16px;
  }
}
.common {
  display: flex;
  justify-content: space-between;
  width: 100%;
  .el-form-item {
    flex: 1;
  }
}
::v-deep .el-dialog {
  margin-top: 8vh !important;
}
.el-icon-circle-plus-outline {
  margin-left: 20px;
  &::before {
    font-size: 24px;
    color: #409eff;
  }
}
.el-icon-delete {
  margin-left: 20px;
  &::before {
    font-size: 24px;
    color: #f56c6c;
  }
}
::v-deep .el-checkbox-button__inner {
  border-left: 1px solid #dcdfe6;
}
::v-deep .el-upload-list__item {
  display: none;
}
::v-deep .el-upload--text {
  width: 100%;
}
::v-deep .el-upload-dragger {
  width: 70%;
}
::v-deep .el-form {
  padding-left: 60px;
}
</style>
